package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: CategoryServiceImpl
 * @Description:
 * @Author: zhangcheng
 * @DateTime: 2022/6/8 16:50
 * @Version 1.0
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类,要先判断分类是否关联菜品和套餐
     *
     * @param id
     * @return
     */
    @Override
    public R delete(Long id) {
        //判断是否关联菜品
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId, id);
        if (dishService.count(lqw1) > 0) {
            return R.error("当前分类关联了菜品,不能删除");
        }
        //判断是否关联套餐
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(lqw2) > 0) {
            return R.error("当前分类关联了套餐,不能删除");
        }
        //未关联,直接删除 调用Iservice里的removeById
        super.removeById(id);
        return R.success("分类删除成功");
    }

}
